<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    //商品添加页面
    public function getAdd()
    { 
      $cates = DB::table('cates')->get();
      return view('admin.goods.add',['cates'=>$cates]);
    }

    //添加商品操作
    public function postInsert(Request $request)
    {
      //表单验证
      $this->validate($request,[
        'gname'=>'required|regex:/^\S{1,30}$/',
        'price'=>'required|regex: /^\d+(\.\d+)?$/',
        'cnt'=>'regex:/^\d{0,6}$/',
        // 'path'=>'image'
        ],[
        'gname.required'=>'请填写商品名',
        'gname.regex'=>'商品名不能超过三十个字符',
        'price.required'=>'请填写价格',
        'price.regex'=>'请填写正确的价格',
        'cnt.regex'=>'请输入正确的库存',
        // 'path.image'=>'请选择正确的图片'
        ]);
      $data = $request->except(['_token','path']);
      //拼接内容
      $data['color'] = implode('@@@',$data['color']);
      $data['size'] = implode('@@@',$data['size']);
      //将数据插入到主表中
      $gid = DB::table('shop_goods')->insertGetId($data);
      //处理图片
      if($request->hasFile('path')){
        //待拼接的数组
        $d = [];
        //获取所有的文件对象
        $files = $request->file('path');
        foreach($files as $k=>$v){
          $fileName = time().rand(100000, 999999);
          $suffix = $v->getClientOriginalExtension();
          //拼接文件名
          $fileName = $fileName.'.'.$suffix;
          //目录
          $dir = './Uploads/'.date('Ymd').'/';
          $v->move($dir, $fileName);
          //拼接图片路径(绝对路径)
          $tmp['path'] = trim($dir.$fileName,'.');
          $tmp['goods_id'] = $gid;
          $d[] = $tmp;
        }
        //将图片信息插入到pics表
        $res = DB::table('pics')->insert($d);
        if($res && $gid){
          return redirect('/goods/index')->with('info','插入成功');
        }else{
          return back()->with('info','插入失败');
        }
       }
      if($gid){
        return redirect('/goods/index')->with('info','插入成功');
      }else{
        return back()->with('info','插入失败');
      }




    }

    public function getIndex(Request $request)
    {
      $goods = DB::table('shop_goods')->orderBy('gid','asc')
      ->select('shop_goods.*','cates.cname as names')
      ->join('cates','cates.cid','=','shop_goods.cate_id')
      ->where(function ($query) use ($request){
      //获取关键字内容
            $k = $request->input('keyword');
            if(!empty($k)) {
                $query->where('gname','like','%'.$k.'%');
            }
        })->paginate($request->input('num', 10));
      return view('admin.goods.index',['goods'=>$goods,'request'=>$request]);
    }

    public function getDelete(Request $request)
    {
      $id = $request->input('gid');
      $res = DB::table('shop_goods')->where('gid',$id)->delete();
      $res2 = DB::table('pics')->where('goods_id',$id)->delete();
      return back();
    }

    //修改
    public function getEdit(Request $request)
    {
      $id = $request->input('gid');
      //获取数据
      $res = DB::table('shop_goods')
      ->select('shop_goods.*','cates.cname as names')
      ->leftJoin('cates','cates.cid','=','shop_goods.cate_id')
      ->where('gid',$id)->first();

      $res2 = DB::table('pics')
      ->select('path')
      ->where('goods_id',$id)->get();
      // 得到size和color的数组对象(json)
      $color = json_encode(explode('@@@', $res->color));
      $size = json_encode(explode('@@@', $res->size));
      //显示页面
      return view('admin.goods.edit',[
        'goods'=>$res,
        'pics'=>$res2,
        'color'=>$color,
        'size'=>$size,
        ]);
    }

    //执行修改操作
    public function postDoedit(Request $request)
    {
         $this->validate($request,[
        'gname'=>'required|regex:/^\S{1,30}$/',
        'price'=>'required|regex: /^\d+(\.\d+)?$/',
        'cnt'=>'regex:/^\d{0,6}$/',
        // 'path'=>'image'
        ],[ 
        'gname.required'=>'请填写商品名',
        'gname.regex'=>'商品名不能超过三十个字符',
        'price.required'=>'请填写价格',
        'price.regex'=>'请填写正确的价格',
        'cnt.regex'=>'请输入正确的库存',
        // 'path.image'=>'请选择正确的图片'
        ]);
      $data = $request->except(['_token','path']);
      $data['color'] = implode('@@@',$data['color']);
      $data['size'] = implode('@@@',$data['size']);
      $res = DB::table('shop_goods')->where('gid',$request->input('gid'))->update($data);
      $gid = $data['gid'];
      //处理文件上传
        if($request->hasFile('path')){
          $aa = DB::table('pics')->where('goods_id',$gid)->delete();
         //待拼接的数组
         $d = [];
         //获取所有的文件对象
         $files = $request->file('path');
         foreach($files as $k=>$v){
            $fileName = time().rand(100000, 999999);
          $suffix = $v->getClientOriginalExtension();
          //拼接文件名
           $fileName = $fileName.'.'.$suffix;
           //目录
           $dir = './Uploads/'.date('Ymd').'/';
          $v->move($dir, $fileName);
           //拼接图片路径(绝对路径)
           $tmp['path'] = trim($dir.$fileName,'.');
           $tmp['goods_id'] = $gid;
           $d[] = $tmp;
         }
         //将图片信息插入到pics表
         $res = DB::table('pics')->insert($d);
           if($res && $gid){
             return redirect('/goods/index')->with('info','插入成功');
           }else{
             return back()->with('info','插入失败');
           }
         }


      //更新
      if($res){
        return redirect('/goods/index')->with('info','修改成功');
      }else{
        return redirect('/goods/index')->with('info','没有任何东西被修改');
      }   


    }


}
